/**
 * 
 */
package fb.chatter.controller;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import fb.chatter.utils.ExceptionUtils;

/**
 * @author Horizon
 * 
 */
public class MainFormController implements Initializable {

	/**
	 * Define JavaFX2.0 components
	 */
	@FXML
	private Parent rootPane;
	@FXML
	private Button btnLogin;
	
	/**
	 * {@link FriendListFormLoader}
	 */
	private FriendListFormLoader friendListLoader;
	
	/**
	 * {@link Thread}
	 */
	private Thread friendListLoaderThread;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {

		Stage stage = new Stage();
		stage.getIcons().add(new Image("/logo-main.png"));
		stage.setTitle("Facebook chatter");
		stage.setScene(new Scene(rootPane, 600, 400));
		stage.setX(50);
		stage.setY(50);
		stage.show();

		btnLogin.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {

				try {
					// Load LoginForm
					showLoginFormStage();
				} catch (IOException e) {
					ExceptionUtils.log(e);
				}
			}
		});
		
		// On close request
		stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
			
			@Override
			public void handle(WindowEvent event) {
				
				// Stop the FriendListLoaderThread
				stopFriendListFormLoaderThread();
			}
		});
	}

	/**
	 * To show the Facebook login form stage
	 * 
	 * @throws IOException
	 */
	private void showLoginFormStage() throws IOException {

		// Load LoginForm
		FXMLLoader fxmlLoader = new FXMLLoader();
		fxmlLoader.load(getClass().getResource("../view/LoginForm.fxml").openStream());
		((LoginFormController) fxmlLoader.getController()).setParentForm(this);
	}
	
	/**
	 * To load the friend list form
	 */
	public void loadFriendListForm(){
		
		startFriendListLoaderThread();
		updateUIWhenLoginSuccessfully();
	}
	
	/**
	 * To load FriendListForm
	 */
	private void startFriendListLoaderThread(){
		
		// Start getting friend list
		friendListLoader = new FriendListFormLoader();
		friendListLoaderThread = new Thread(friendListLoader);
		friendListLoaderThread.setDaemon(true);
		friendListLoaderThread.start();
	}
	
	/**
	 * To stop the FriendListForm loader
	 */
	private void stopFriendListFormLoaderThread(){
		
		if (friendListLoader != null) {
			try {
				friendListLoaderThread.checkAccess();
				friendListLoaderThread.interrupt();
			} catch (SecurityException e) {
				ExceptionUtils.log(e);
			}
		}
	}
	
	/**
	 * To update UI when login successfully
	 */
	private void updateUIWhenLoginSuccessfully(){
		
		// Disable Login button on the MainForm
		Button btnLogin = (Button) this.rootPane.lookup("#btnLogin");
		btnLogin.setDisable(true);
	}
}
